package com.hwadee.ems.controller;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;

import com.hwadee.ems.domain.dto.PageQuery;
import com.hwadee.ems.domain.dto.Result;
import com.hwadee.ems.domain.po.TrainingProgram;
import com.hwadee.ems.domain.vo.TrainingProgramVO;
import com.hwadee.ems.service.ITrainingProgramService;


/**
 * <p>
 * 培养方案 前端控制器
 * </p>
 *
 * @author EnosElinsa
 * @since 2024/06/21
 */
@Slf4j
@RestController
@RequestMapping("/training_program")
public class TrainingProgramController {

    @Resource
    private ITrainingProgramService trainingProgramService;

    @GetMapping("/page")
    public Result<IPage<TrainingProgramVO>> page(PageQuery pageQuery) {
        return trainingProgramService.page(pageQuery);
    }

    @PostMapping
    public Result<Long> save(@RequestBody TrainingProgram trainingProgram) {
        log.info("Saving training program: {}", trainingProgram);
        if (trainingProgramService.save(trainingProgram)) {
            return Result.ok(trainingProgram.getId());
        } else {
            log.error("Failed to save training program: {}", trainingProgram);
            return Result.error();
        }
    }

    @PutMapping
    public Result<Void> updateById(@RequestBody TrainingProgram trainingProgram) {
        if (trainingProgramService.updateById(trainingProgram)) {
            log.info("Updated training program by ID: {}", trainingProgram);
            return Result.ok();
        } else {
            log.error("Failed to update training program by ID: {}", trainingProgram);
            return Result.error();
        }
    }
    
    @DeleteMapping("/{id}")
    public Result<Void> removeById(@PathVariable Long id) {
        if (trainingProgramService.removeById(id)) {
            log.info("Removed training program by ID: {}", id);
            return Result.ok();
        } else {
            log.error("Failed to remove training program by ID: {}", id);
            return Result.error();
        }
    }

    @GetMapping("/page_by_major_id")
    public Result<IPage<TrainingProgramVO>> pageByMajorId(@RequestParam Long majorId, PageQuery pageQuery) {
        return trainingProgramService.pageByMajorId(majorId, pageQuery);
    }

    @GetMapping("/{student_id}")
    public Result<TrainingProgramVO> getByStudentId(@PathVariable("student_id") Long studentId) {
        return trainingProgramService.getByStudentId(studentId);
    }

}